package woaini.wind.bot.core.adapter.hook;
import woaini.wind.bot.core.event.enums.EventType;
import woaini.wind.bot.core.event.segment.Messages;
import woaini.wind.bot.core.event.base.Self;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import woaini.wind.bot.core.adapter.HttpAdapter;
import woaini.wind.bot.core.event.message.impl.PrivateMessageEvent;

import java.util.HashMap;
import java.util.Map;

/**
 * 公共的http请求接口
 *
 * @see CommonHttpHook
 * @author yefeng {@date 2024-12-27 15:03:52}
 */
@RestController
@RequestMapping("/wind/hook")
@Slf4j
public class CommonHttpHook {

  /**
   * @see Map<String, HttpAdapter> 适配器列表
   */
  public static final Map<String, HttpAdapter> HTTP_ADAPTERS = new HashMap<>();

  public static void addHttpAdapter(HttpAdapter httpAdapter) {
    CommonHttpHook.HTTP_ADAPTERS.put(httpAdapter.getBot().agreement(), httpAdapter);
  }

  @RequestMapping("/{code}")
  public void hook(@PathVariable("code") String code,@RequestBody JSONObject body){
    log.info("httpHook:{}",body.toJSONString());
    //获取对应的适配器
    HttpAdapter httpAdapter = CommonHttpHook.HTTP_ADAPTERS.get(code);
    if (httpAdapter == null){
      log.warn("未找到适配器");
      return;
    }
    httpAdapter.httpHook(body.toJSONString());
  }
}
